package com.baizhi.lql.service;

import com.baizhi.lql.dao.UserMapper;
import com.baizhi.lql.entity.User;
import com.baizhi.lql.entity.UserExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userdao;

    @Override
    public Map<String, Object> modify(int id,String status,User user) {
        Map<String, Object> map = new HashMap<>();
        user.setId(id);
        user.setStatus(status);
        int i = userdao.updateByPrimaryKeySelective(user);

        if(i==1){
            userdao.updateByPrimaryKeySelective(user);
            map.put("message","操作成功");
            map.put("status",200);
        }else {
            map.put("message", "操作失败");
            map.put("status", 400);
        }
        return map;
    }

    @Override
    public User queryOne(int id) {
        User user = userdao.selectByPrimaryKey(id);
        return user;
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryAllPage(int page, int pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        int offset = (page - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<User> users = userdao.selectByExampleAndRowBounds(example,rowBounds);
        int count = userdao.selectCount(null);

        map.put("page",page);
        map.put("rows",users);
        map.put("total",count);

        return map;
    }

}
